<template>
  <a-drawer
    :title="title"
    :closable="false"
    :maskClosable="false"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :md="12" :sm="12">
            <a-form-item
              label="学期时间范围"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-date-picker placeholder="请输入开始时间" format="YYYY-MM-DD" :disabled-date="disabledStartDate"
                             v-decorator="['startDate', {rules: [{required: true, message: '请选择开始时间！'}]}]"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="12">
            <a-form-item
              label=""
              has-feedback
            >
              <a-date-picker placeholder="请输入结束时间" format="YYYY-MM-DD" :disabled-date="disabledEndDate"
                             v-decorator="['endDate', {rules: [{required: true, message: '请选择结束时间！'}]}]"/>
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24">
            <a-form-item
              label="学期名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input
                placeholder="请输入学期名称"
                v-decorator="['name', {rules: [{required: true, message: '请输入学期名称！'}]}]"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
    <div class="drawer-footer">
      <a-button class="left" @click="handleCancel">
        关闭
      </a-button>
      <a-button class="right" type="primary" @click="handleSubmit">
        提交
      </a-button>
    </div>
  </a-drawer>
</template>

<script>
  import moment from 'moment'
  import { sysSchoolTermAdd, sysSchoolTermEdit } from '@/api/modular/system/sysSchoolTermManage'

  export const dateFormat = 'YYYY-MM-DD'
  export default {
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        id: '',
        title: ''
      }
    },
    created() {

    },
    methods: {
      // 初始化方法
      open(record) {
        if (record) {
          this.id = record.id
          this.title = '编辑学期信息'
          setTimeout(() => {
            this.form.setFieldsValue(
              {
                id: record.id,
                startDate: moment(record.startDate, dateFormat),
                endDate: moment(record.endDate, dateFormat),
                name: record.name
              }
            )
          }, 100)
        } else {
          this.id = ''
          this.title = '新增学期信息'
        }
        this.visible = true
      },
      handleSubmit() {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          debugger
          if (!errors) {
            console.log(values)
            const data = { ...values }
            data.startDate = moment(data.startDate).format(dateFormat)
            data.endDate = moment(data.endDate).format(dateFormat)
            if (this.id) {
              data.id = this.id
              sysSchoolTermEdit(data).then((res) => {
                if (res.success) {
                  this.$message.success('更新学期信息成功')
                  this.confirmLoading = false
                  this.$emit('ok', data)
                  this.handleCancel()
                } else {
                  this.$message.error('更新系学期信息失败：' + res.message)
                }
              }).finally((res) => {
                this.confirmLoading = false
              })
            } else {
              sysSchoolTermAdd(data).then((res) => {
                if (res.success) {
                  this.$message.success('新增学期信息成功')
                  this.confirmLoading = false
                  this.$emit('ok', data)
                  this.handleCancel()
                } else {
                  this.$message.error('新增学期信息失败：' + res.message)
                }
              }).finally((res) => {
                this.confirmLoading = false
              })
            }
          } else {
            this.confirmLoading = false
            this.$message.error(errors)
          }
        })
      },
      handleCancel() {
        this.form.resetFields()
        this.visible = false
      },
      disabledStartDate(startValue) {
        const endValue = this.form.getFieldValue('endDate')
        if (!startValue || !endValue) {
          return false
        }
        return startValue.valueOf() > endValue.valueOf()
      },
      disabledEndDate(endValue) {
        const startValue = this.form.getFieldValue('startDate')
        if (!endValue || !startValue) {
          return false
        }
        return startValue.valueOf() >= endValue.valueOf()
      }
    }
  }
</script>
